package com.khalil.kkadmin.infrastructure;

import com.khalil.kkadmin.common.web.ReqBody;
import com.khalil.kkadmin.common.web.RespResult;
import com.khalil.kkadmin.infrastructure.jpa.domain.EntityBase;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class BaseController<S extends IService<T, K>, T extends EntityBase<K>, K> {

    protected final S service;

    public BaseController(S service) {
        this.service = service;
    }

    @GetMapping("/{id}")
    public RespResult<T> findById(@PathVariable(name = "id") K id) {
        T data = service.findById(id);
        return RespResult.ok(data);
    }

    @PostMapping("/list")
    public RespResult<List<T>> findList(@RequestBody ReqBody<String> reqBody) {
        String params = reqBody.getData();
        return RespResult.ok(service.list(params));
    }

    @PostMapping
    public RespResult<T> create(@RequestBody ReqBody<String> reqBody) {
        return RespResult.ok(service.create(reqBody.getData()));
    }

    @PutMapping
    public RespResult<T> update(@RequestBody ReqBody<String> reqBody) {
        return RespResult.ok(service.update(reqBody.getData()));
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable(name = "id") K id) {
        service.deleteById(id);
    }
}
